<?php

namespace App\Console\Commands;

use App\Library\SwooleHandler;
use Illuminate\Console\Command;

class swoole extends Command {
    public $server;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'swoole';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     *
     */
    public function __construct() {
        parent::__construct();

    }

    public function start(){
        $this->server = new \swoole_websocket_server('0.0.0.0', '9502');
        $this->server->set(array(
            'worker_num' => 1,
            'dispatch_mode' => 2, //数据包分发策略。可以选择3种类型，默认为2  

        ));
        $handler=new SwooleHandler();
        $this->server->on('request',[$handler,'onRequest']);
        $this->server->on('open',[$handler,'onOpen']);
        $this->server->on('message',[$handler,'onMessage']);
        $this->server->on('close',[$handler,'onClose']);
        $this->server->on('WorkerStart',[$handler,'onWorkerStart']);
        $this->server->start();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {
           $this->start();
    }
}
